package com.zphhhhh.speech;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public final class SpeechUtil {

    private static final int PART_LEN = 2048;

    public static String[] split(String str) {
        int type = 0;

        int n1 = countChar(str, ','); // 英文逗号
        int n2 = countChar(str, '，'); // 中文逗号

        if (n1 < n2) {
            type = 1; // 中文
        } else {
            type = 2; // 英文
        }

        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();

        int beginIndex, endIndex, spIndex;

        beginIndex = 0;
        endIndex = beginIndex + PART_LEN;
        spIndex = findSplitPoint(str, beginIndex, endIndex, type);
        list1.add(beginIndex);
        list2.add(spIndex);

        if (str.length() - spIndex <= PART_LEN) {
            list1.add(spIndex + 1);
            list2.add(str.length() - 1);
        } else {
            while (str.length() - spIndex > PART_LEN) {
                beginIndex = spIndex + 1;
                endIndex = beginIndex + PART_LEN;
                spIndex = findSplitPoint(str, beginIndex, endIndex, type);
                list1.add(beginIndex);
                list2.add(spIndex);
                if (str.length() - spIndex < PART_LEN) {
                    list1.add(spIndex + 1);
                    list2.add(str.length() - 1);
                    break;
                }
            }
        }

        Log.d("ReactNative", "partArr[i]: str.length() = " + str.length());
        String[] partArr = new String[list1.size()];
        for (int i = 0; i < list1.size(); i++) {
            int begin = list1.get(i);
            int end = list2.get(i);
            partArr[i] = str.substring(begin, end);
            Log.d("ReactNative", "partArr[i]: " + i + " " + begin + " " + end + " " + partArr[i].length());
            Log.d("ReactNative", "partArr[i]: " + partArr[i]);
        }

        return partArr;
    }

    // 计算某个字符的个数
    private static int countChar(String str, int ch) {
        int count = 0;

        int beginIndex = 0;
        int findIndex = -1;
        while (true) {
            findIndex = str.indexOf(ch, beginIndex);
            if (findIndex == -1) {
                // 找不到，跳出来
                break;
            } else {
                // 找到，计数
                count++;
                // 更新查找索引值，继续找
                beginIndex = findIndex + 1;
            }
        }
        return count;
    }

    // 找到分割点
    private static int findSplitPoint(String str, int beginIndex, int endIndex, int type) {
        char ch;
        for (int i = endIndex; i > endIndex - 512; i--) {
            ch = str.charAt(i);
            if (ch == '\n') {
                return i;
            } else if (type == 1 && ch == '。') {
                return i;
            } else if (type == 2 && ch == '.') {
                char nextChar = str.charAt(i + 1);
                if (nextChar == ' ' || Character.isUpperCase(nextChar))
                    return i;
            }
        }
        return endIndex;
    }

}
